{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting data/train-images-idx3-ubyte.gz\n",
      "Extracting data/train-labels-idx1-ubyte.gz\n",
      "Extracting data/t10k-images-idx3-ubyte.gz\n",
      "Extracting data/t10k-labels-idx1-ubyte.gz\n",
      "MNIST loaded\n"
     ]
    }
   ],
   "source": [
    "mnist      = input_data.read_data_sets('data/', one_hot=True)\n",
    "trainimg   = mnist.train.images\n",
    "trainlabel = mnist.train.labels\n",
    "testimg    = mnist.test.images\n",
    "testlabel  = mnist.test.labels\n",
    "print (\"MNIST loaded\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(55000, 784)\n",
      "(55000, 10)\n",
      "(10000, 784)\n",
      "(10000, 10)\n",
      "[ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]\n"
     ]
    }
   ],
   "source": [
    "print (trainimg.shape)\n",
    "print (trainlabel.shape)\n",
    "print (testimg.shape)\n",
    "print (testlabel.shape)\n",
    "#print (trainimg)\n",
    "print (trainlabel[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = tf.placeholder(\"float\", [None, 784]) \n",
    "y = tf.placeholder(\"float\", [None, 10])  # None is for infinite \n",
    "W = tf.Variable(tf.zeros([784, 10]))\n",
    "b = tf.Variable(tf.zeros([10]))\n",
    "# LOGISTIC REGRESSION MODEL\n",
    "actv = tf.nn.softmax(tf.matmul(x, W) + b) \n",
    "# COST FUNCTION\n",
    "cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1)) \n",
    "# OPTIMIZER\n",
    "learning_rate = 0.01\n",
    "optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# PREDICTION\n",
    "pred = tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))   \n",
    "# ACCURACY\n",
    "accr = tf.reduce_mean(tf.cast(pred, \"float\"))\n",
    "# INITIALIZER\n",
    "init = tf.global_variables_initializer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 5, 2, 1, 3, 0], dtype=int64)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sess = tf.InteractiveSession()\n",
    "\n",
    "arr = np.array([[31, 23,  4, 24, 27, 34],\n",
    "                [18,  3, 25,  0,  6, 35],\n",
    "                [28, 14, 33, 22, 20,  8],\n",
    "                [13, 30, 21, 19,  7,  9],\n",
    "                [16,  1, 26, 32,  2, 29],\n",
    "                [17, 12,  5, 11, 10, 15]])\n",
    "#tf.rank(arr).eval()\n",
    "#tf.shape(arr).eval()\n",
    "#tf.argmax(arr, 0).eval()\n",
    "# 0 -> 31 (arr[0, 0])\n",
    "# 3 -> 30 (arr[3, 1])\n",
    "# 2 -> 33 (arr[2, 2])\n",
    "tf.argmax(arr, 1).eval()\n",
    "# 5 -> 34 (arr[0, 5])\n",
    "# 5 -> 35 (arr[1, 5])\n",
    "# 2 -> 33 (arr[2, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 000/050 cost: 1.177906594 train_acc: 0.840 test_acc: 0.855\n",
      "Epoch: 005/050 cost: 0.440515266 train_acc: 0.860 test_acc: 0.895\n",
      "Epoch: 010/050 cost: 0.382895913 train_acc: 0.910 test_acc: 0.905\n",
      "Epoch: 015/050 cost: 0.356607343 train_acc: 0.870 test_acc: 0.909\n",
      "Epoch: 020/050 cost: 0.341326642 train_acc: 0.860 test_acc: 0.912\n",
      "Epoch: 025/050 cost: 0.330556413 train_acc: 0.910 test_acc: 0.913\n",
      "Epoch: 030/050 cost: 0.321508561 train_acc: 0.840 test_acc: 0.916\n",
      "Epoch: 035/050 cost: 0.314936944 train_acc: 0.940 test_acc: 0.917\n",
      "Epoch: 040/050 cost: 0.309805418 train_acc: 0.940 test_acc: 0.918\n",
      "Epoch: 045/050 cost: 0.305343132 train_acc: 0.960 test_acc: 0.918\n",
      "DONE\n"
     ]
    }
   ],
   "source": [
    "training_epochs = 50\n",
    "batch_size      = 100\n",
    "display_step    = 5\n",
    "# SESSION\n",
    "sess = tf.Session()\n",
    "sess.run(init)\n",
    "# MINI-BATCH LEARNING\n",
    "for epoch in range(training_epochs):\n",
    "    avg_cost = 0.\n",
    "    num_batch = int(mnist.train.num_examples/batch_size)\n",
    "    for i in range(num_batch): \n",
    "        batch_xs, batch_ys = mnist.train.next_batch(batch_size)\n",
    "        sess.run(optm, feed_dict={x: batch_xs, y: batch_ys})\n",
    "        feeds = {x: batch_xs, y: batch_ys}\n",
    "        avg_cost += sess.run(cost, feed_dict=feeds)/num_batch\n",
    "    # DISPLAY\n",
    "    if epoch % display_step == 0:\n",
    "        feeds_train = {x: batch_xs, y: batch_ys}\n",
    "        feeds_test = {x: mnist.test.images, y: mnist.test.labels}\n",
    "        train_acc = sess.run(accr, feed_dict=feeds_train)\n",
    "        test_acc = sess.run(accr, feed_dict=feeds_test)\n",
    "        print (\"Epoch: %03d/%03d cost: %.9f train_acc: %.3f test_acc: %.3f\" \n",
    "               % (epoch, training_epochs, avg_cost, train_acc, test_acc))\n",
    "print (\"DONE\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
